package top.mengchaob.gitManage.controller;

import org.eclipse.jgit.api.errors.GitAPIException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.mengchaob.exception.NotMavenPackageException;
import top.mengchaob.exception.RelationBranchException;
import top.mengchaob.exception.RemoteBranchNotExistException;
import top.mengchaob.gitManage.api.GitManageServiceI;
import top.mengchaob.gitManage.util.GitUtil;
import top.mengchaob.util.PropUtil;
import top.mengchaob.util.ResultBean;

import java.util.List;

/**
 * @title：
 * @author: mengchaob
 * @date: 2021年05月31日 20:31
 * @description:
 */
@Controller
@RequestMapping("gitManageController")
public class GitManageController {
    @Autowired
    GitManageServiceI gitManageServiceI;
    /**
     * 获取项目所有分支
     * @return
     */
    @RequestMapping("getBranchAndTag")
    @ResponseBody
    public ResultBean getBranchAndTag(){
        GitUtil gitUtil = new GitUtil(PropUtil.getV("git.name"),PropUtil.getV("git.password"),PropUtil.getV("git.workSpace"));
        List list = gitUtil.getRemotHeadAndTags();
        return ResultBean.ok(list);
    }
    /**
     * 获取项目所有分支
     * @return
     */
    @RequestMapping("getGitFileByBranch")
    @ResponseBody
    public ResultBean getGitFileByBranch(@RequestParam("branchName")String branchName){
        List fileNames = gitManageServiceI.getGitFileByBranch(branchName);
        return ResultBean.ok(fileNames);
    }

    /**
     * check分支
     * @param branchName
     * @return
     */
    @RequestMapping("checkOutBranch")
    @ResponseBody
    public ResultBean checkOutBranch(@RequestParam("branchName")String branchName){

        return  null;
    }

    /**
     * 更新分支
     * @param branchName
     * @return
     */
    @RequestMapping("pullBranch")
    @ResponseBody
    public ResultBean pullBranch(@RequestParam("branchName")String branchName){
        return null;
    }

    /**
     * 获取当前分支名称
     * @return
     */
    @RequestMapping("getCurrentBranch")
    @ResponseBody
    public ResultBean getCurrentBranch(){
        return null;
    }

    /**
     * 清空分支
     * @return
     */
    @RequestMapping("cleanBranch")
    @ResponseBody
    public ResultBean cleanBranch(){
        return null;
    }
    /**
     * 分支打包
     * @return
     */
    @RequestMapping("packageBranch")
    @ResponseBody
    public ResultBean packageBranch(@RequestParam("branchName")String branchName){
        ResultBean rs = null;
        try {
            gitManageServiceI.packageBranch(branchName);
            rs = ResultBean.ok();
        } catch (RelationBranchException e) {
            e.printStackTrace();
            rs = ResultBean.error(e.getMessage());
        } catch (RemoteBranchNotExistException e) {
            e.printStackTrace();
            rs = ResultBean.error(e.getMessage());
        }
        return rs;
    }
    /**
     * 克隆
     * @return
     */
    @RequestMapping("gitclone")
    @ResponseBody
    public ResultBean gitclone(){
        ResultBean rs = null;
        try {
            gitManageServiceI.gitClone();
            rs = ResultBean.ok();
        } catch (GitAPIException e) {
            e.printStackTrace();
        }
        return rs;
    }
}
